<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>活动管理</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/icon.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/demo.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/admin/css/form.css"/>
	
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.min.js"></script>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.easyui.min.js"></script>
	<script src="${pageContext.request.contextPath}/plugins/jQuery/easyui-lang-zh_CN.js"></script>
	
	<script src="${pageContext.request.contextPath}/admin/js/reg.js"></script>
	
	<script type="text/javascript">
		$(function(){
		     
			loadTable();
			$('#dlg').dialog('close');
			
			//加载
            $('body').append('<div id="myWindow" class="easyui-dialog" closed="true"></div>'); 
            
            $('body').append('<div id="chargerd" class="easyui-dialog" closed="true"></div>'); 
            $('body').append('<div id="transrd" class="easyui-dialog" closed="true"></div>'); 
			
		});
		
		function loadTable(){
		   
		       //设置时间  
// 			var curr_time = new Date();     
// 		  	var s = $("#start_time").textbox('getValue');
// 		   	var e = $("#end_time").textbox('getValue');
		   
// 		   	if(s != null && s != '' && e !=null && e!=''){
// 		       $("#start_time").textbox("setValue",s);
// 		       $("#end_time").textbox("setValue",e);
// 		   	}else{
// 		       $("#start_time").textbox("setValue",myformatter(curr_time)); 
// 		       $("#end_time").textbox("setValue",myformatter(curr_time));
// 		   	}
		
			$('#dg').datagrid({
				rownumbers:true,
				singleSelect:true,
				pagination:true,
				pageSize:10,
				pageList:[10,20,30,50,100],
		 		url:'${pageContext.request.contextPath }/admin/activity/activitylist',
		 		queryParams:{
		 		   start_time:$("#start_time").textbox('getValue'),
		 		   end_time:$("#end_time").textbox('getValue')
		 		},
	    		columns:[[
					{field:'activity_name',title:'活动名称',align:'center',width:150},
					{field:'start_time',title:'活动开始时间',align:'center',width:150},
					{field:'end_time',title:'活动结束时间',align:'center',width:150},
					{field:'activity_statename',title:'状态',align:'center',width:100},
					{field:'remarks',title:'备注',align:'center',width:200},
					{field:'operation',title:'操作选项',width:320,align:'left',formatter: function (value, row, index) {
						var str = '';
						str+=' <a href="javascript:edit(\''+row.id+'\')" name="edit"  class="easyui-linkbutton" ></a>';
						
						if(row.activity_state == '00'){
						   str+=' <a href="javascript:enable(\''+row.id+'\')" name="enable"  class="easyui-linkbutton" ></a>';
						}else if(row.activity_state == '01'){
						   str+=' <a href="javascript:disabled(\''+row.id+'\')" name="disabled"  class="easyui-linkbutton" ></a>';
						}
						return str;
					}}
	 			]],
				onLoadSuccess:function(data){  
			        $("a[name='edit']").linkbutton({text:'编辑',plain:true,iconCls:'icon-my-enable'});
			        $("a[name='enable']").linkbutton({text:'启用',plain:true,iconCls:'icon-my-enable'});
			        $("a[name='disabled']").linkbutton({text:'禁用',plain:true,iconCls:'icon-my-disable'});
				},
				toolbar: [{
						text:'添加活动',
						iconCls:'icon-person-add',
						handler:function(){
							activityadd();
						 }
					   }]
			});
		}

		function disabled(id){
		  $.post("${pageContext.request.contextPath}/admin/activity/activitystate",{id:id,activity_state:'00'},
				function(data){
					if(data=="0"){
						$.messager.alert('系统消息','<span style=\"color:red;\">充值活动禁用失败，请稍后再试！</span>');	 
					}else{
						$.messager.alert('系统消息','充值活动禁用成功！');
						loadTable();
					}
				},"text");
		}
		
		function enable(id){
		  $.post("${pageContext.request.contextPath}/admin/activity/activitystate",{id:id,activity_state:'01'},
				function(data){
					if(data=="0"){
						$.messager.alert('系统消息','<span style=\"color:red;\">充值活动启用失败，请稍后再试！</span>');	 
					}else{
						$.messager.alert('系统消息','充值活动启用成功！');
						loadTable();
					}
				},"text");
		}
		
	    var url;
		function activityadd(){
		    
			$('#dlg').dialog('open').dialog('setTitle','新增');
		    $('#fm').form('clear');
		    
		    url="${pageContext.request.contextPath}/admin/activity/activityadd";
		}
		
	    function saveobj(){
	        
	        if($("#activity_name").val()==null||$("#activity_name").val()==""){
				$.messager.alert('提示','活动名称必须输入！');
				return;
			}
			var formdata = $('#fm').serialize();
			$.ajax({
			type: "POST",
			url: url,
			data:formdata,// 要提交表单的ID
			success: function(result){
					if(result==0){
						$('#dlg').dialog('close');		// close the dialog
						$.messager.show({title:'活动',msg:'保存成功！',timeout:6,showType:'slide'});
						$('#dg').datagrid('reload');	// reload the user data
					}else{
					  $.messager.alert('系统消息','<span style=\"color:red;\">提交表单失败，请稍后再试！</span>');
					}
			},
			error : function(msg) {
				console.info(msg);
			}
		   });
	    }
	    
	 function edit(id){
	    $('#dlg').dialog('open').dialog('setTitle','编辑');;
		$('#fm')[0].reset();
		generateDialog(id);
		url="${pageContext.request.contextPath}/admin/activity/activityedit";
     }
    
    //设置弹出框信息 
	  function generateDialog(id){
		var url = "${pageContext.request.contextPath}/admin/activity/activityview?id="+id+"&v="+Math.random();
		$.ajax({
				 type : "get",
				 url : url,
				 async : false,
				 cache:false, 
				 dataType:"json",
				 success : function(data){
				 
					$("#id").val(data.id);
					$("#activity_name").textbox('setValue',data.activity_name);
					$("#p_start_time").textbox('setValue',data.start_time);
					$("#p_end_time").textbox('setValue',data.end_time);
				 }
			});
	   }
		
	   function showImg(imgUrl){
			$('#dlg').dialog('open');
			var image=document.getElementById("stationImg");
			var src="${pageContext.request.contextPath }"+imgUrl;
			image.setAttribute("src",src );
		}

		// 打开一个新的选项卡，并制定选项卡的名称
		function show(url,name){
			parent.window.addTab(name,url);
		}
		
		function findData(){
			loadTable();
		}
		
       function myformatter(date){  
		      var y = date.getFullYear();  
		      var m = date.getMonth()+1;  
		      var d = date.getDate();  
		      return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);  
	   }  
	          
	   function myparser(s){  
		    if (!s) return new Date();  
		    var ss = (s.split('-'));  
		    var y = parseInt(ss[0],10);  
		    var m = parseInt(ss[1],10);  
		    var d = parseInt(ss[2],10);  
		    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){  
		        return new Date(y,m-1,d);  
		    } else {  
		        return new Date();  
		    }  
	  }

	</script>
	<style type="text/css">
		body{
		font-family:'微软雅黑';}
	</style>
  </head>
  
  <body>
	<table id="dg" title="活动管理" toolbar="#searchtool" loadMsg="正在查询..." style="width:90%;height:700px;">
	   <div id="searchtool" style="padding:5px; height:auto;">  
	      <span style="margin:0px 12px;">时间:</span><input id="start_time" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" style="width:120px"> -- <input id="end_time" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" style="width:120px">
          <a href="javascript:findData()" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:100px;margin:0px 3px;">查询</a>
          <a href="javascript:findData()" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" style="width:100px;margin:0px 3px;">刷新</a>
       </div>
	</table>
	
   <div id="dlg" class="easyui-dialog" style="width:750px;height:500px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
	 <div class="ftitle">活动</div>
	  <form id="fm" method="post">
	
			<div class="fitem">
				<label>活动名称:</label>
				<input id="activity_name" name="activity_name" class="easyui-textbox" style="width:200px;height:26px"/>
				
				<input id="id" name="id" type="hidden" />
				
			</div>
			
			<div class="fitem">
				<label>活动开始时间:</label>
				<input id="p_start_time" name="p_start_time" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" style="width:200px">
			</div>
			
			<div class="fitem">
				<label>活动结束时间:</label>
				<input id="p_end_time" name="p_end_time" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" style="width:200px">
				
			</div>
		</form>
		
  </div>
  <div id="dlg-buttons">
	<a href="#" class="easyui-linkbutton" id="add" iconCls="icon-ok" onclick="saveobj()">保存</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">关闭</a>
  </div>
    
  </body>
</html>
